var  BASE = BASE||{}; //全局公用方法
BASE.VIDEO = (function () {
    var SCOKET;
    var DP;

    /*播放视频*/
    function playVideo(obj) {
        BASE.getHttp('/device/videoInfo/play',{ videoInfoId:BASE.COMMON.getId() }, function (res) {
            if(res.code!== 200){
                BASE.clearpop(res.message || '播放出错');
                $('#content .video-content .tips').text(res.message || '播放出错');
                return;
            }
            if(!res.data){
                BASE.clearpop('视频不存在');
                return;
            }
            initVideoScoket(res.data.url);
            $(obj).find('.video').show();
            $(obj).remove();
            BASE.VIDEO_PLAY.iniPage();
        })
    }

    /*播放视频回放*/
    function playVideoBack(obj) {
        BASE.apiGet('/device/onlineRecInfo/detail',{ id:BASE.COMMON.getId() }, function (res) {
            if(!res){
                BASE.clearpop('视频不存在');
                return;
            }
            initVideoScoket(res);
            $(obj).find('.video').show();
            $('#onlineVideo').remove();
            $('#openVideo').remove();
        })
    }
    /*初始化scoket*/
    function _initScoket() {
        if(SCOKET){
            return;
        }
        SCOKET = new SockJS(BASE.baseUrlLocal+'/videows');
        _getMessage();
        _closeScoket();
    }
    /*发送消息*/
    function sendMessage(obj) {
        if(!BASE.COMMON.getUserId()){
            BASE.clearpop('请先登录');
            return;
        }
        if(!DP || DP.paused){
            BASE.clearpop('视频未播放');
            return;
        }
        if(!SCOKET || SCOKET.readyState!==1){
            BASE.clearpop('弹幕连接失败');
            return;
        }
        var val = $(obj).prev().val();
        var flag = $(obj).data('entityid');
        var sendData = {
            entityId: flag+ '_' + BASE.COMMON.getId(),
            body: val,
        };
        if (!val) {
            BASE.clearpop('请输入内容');
            $(obj).prev().focus();
            return;
        }
        SCOKET.send(JSON.stringify(sendData));
        $(obj).prev().val('');
    }

    /*关闭连接*/
    function _closeScoket() {
        window.onbeforeunload = function () {
            DP.destroy();
            SCOKET.close();
        };
    }
    /*接收消息*/
    function _getMessage() {
        var getRandomColor = function(){
            return '#'+('00000'+(Math.random()*0xffffff <<0).toString(16)).substr(-6);
        };
        SCOKET.onmessage = function (e) {
            var data = e.data;
            DP.danmaku.draw({
                text: data,
                color:getRandomColor(),
                type: 'top',
            });
        };
    }

    /*初始化播放器*/
    function _initPlayer(url) {
        var dom = document.querySelector('#dplayer');
        if(DP){
            DP.destroy();
        }
        DP = new DPlayer({
            container: document.querySelector('#dplayer'),
            autoplay: true,
            theme: '#3999f2',
            loop: false,
            lang: 'zh-cn',
            logo: '../../../public/images/logo_nobg.png',
            volume: 0.7,
            video: {
                url: url,
            },
           /* danmaku: {
               /!* id:BASE.COMMON.getId(),
                api: 'https://api.prprpr.me/dplayer3/'*!/
            },*/
        });
        $(dom).show();
        DP.play();
      /*  _initScoket();*/
    }
    /*视频初始化及scoket*/
    function initVideoScoket(data) {
        _initPlayer(data);
    }
    return {
        sendMessage:sendMessage,
        playVideo:playVideo,
        playVideoBack:playVideoBack,
        initVideoScoket:initVideoScoket,
    }
})();